import 'package:enjoy_plus_hm/pages/home/components/home_list.dart';
import 'package:enjoy_plus_hm/pages/home/components/home_nav.dart';
import 'package:enjoy_plus_hm/utils/request.dart';
import 'package:enjoy_plus_hm/utils/toast.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  //首页通知列表数据
  List notifyList = [];

  @override
  void initState() {
    super.initState();
    getNotifyList();
  }

  getNotifyList() async {
    try {
      //测试调用接口  get请求
      // final res = await http.get('/announcement');
      // print(res);

      //测试调用接口  request请求
      final res = await http.request('get', '/announcement', {}, {});
      if (kIsWeb) {
        // ignore: use_build_context_synchronously
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text('获取成功!', textAlign: TextAlign.center)),
        );
      } else {
        ToastUtil.showSuccess('请求成功');
      }

      // print(res);
      setState(() {
        notifyList = res['data'];
      });
    } catch (e) {
      ToastUtil.showError('获取失败');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color.fromARGB(40, 85, 145, 175),
      appBar: AppBar(
        title: const Text('享+社区'),
        backgroundColor: Colors.transparent,
        centerTitle: true,
      ),
      body: ListView(
        scrollDirection: Axis.vertical,
        padding: const EdgeInsets.all(10),
        children: [
          // 导航条
          HomeNav(),
          // 中间广告图
          ClipRRect(
            borderRadius: BorderRadius.circular(10.0),
            child: Image.asset('assets/images/banner@2x.jpg'),
          ),
          // 社区公告
          HomeList(notifyList: notifyList),
        ],
      ),
    );
  }
}
